package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    @RequestMapping("/findOrderDate")
    public Result findOrderDate(){
        return orderService.findOrderDate();
    }

    @RequestMapping("/findOrderDateValue")
    public Result findOrderDateValue(@RequestParam List<String> value1){
        try {
            Map<String,Object> map = new HashMap<>();
            String start = value1.get(0);
            String end = value1.get(1);

            Date sd = DateUtils.parseString2Date(start);
            Date ed = DateUtils.parseString2Date(end);
            Date today = new Date();
            if (sd.getTime()>today.getTime() || ed.getTime()>today.getTime()){
                return new Result(false,"未来不可预测哦");
            }

            List<String> dayList = DateUtils.getPerDaysByStartAndEndDate(start, end, "yyyy-MM-dd");
            map.put("dayList",dayList);
            List<Integer> orderList = new ArrayList<>();
            for (String day : dayList) {
                Integer count = orderService.findOrderDateValueSubscribe(day);
                orderList.add(count);
            }
            map.put("orderList",orderList);

            List<Integer> orderStatus = new ArrayList<>();
            for (String day : dayList) {
                Integer count = orderService.findOrderDateValueUnsubscribe(day);
                orderStatus.add(count);
            }
            map.put("orderStatus",orderStatus);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
